﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyModbus;
using EasyModbus.Exceptions;


namespace _8轴运动控制
{
    public class Mitsubishi
    {
        //创建连接对象
        public ModbusClient mc = new ModbusClient();
        private bool IsConn = false;

        //连接PLC
        public bool Conncet(string IPAdress, string Port)
        {
            Disconncet();
            try
            {
                mc.Connect(IPAdress, Convert.ToInt32(Port));
                IsConn = true;
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //断开连接
        public bool Disconncet()
        {
            try
            {
                mc.Disconnect();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //写M1线圈
        public bool WriteSingle(string str, bool value)
        {
            if (IsConn == false)
            {
                return false;
            }
            try
            {
                int address = DecodM(str);
                mc.WriteSingleCoil(address, value);//写M1
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        //读单个M1线圈
        public bool ReadSingle(string str)
        {
            if (IsConn == false)
            {
                return false;
            }
            try
            {
                int address = DecodM(str);
                bool[] value = mc.ReadCoils(address, 1);
                if (value[0] == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }



        }
        //写D0寄存器
        public bool WriteRegister(string str, int value)
        {
            if (IsConn == false)
            {
                return false;
            }
            try
            {
                int address = DecodM(str);
                mc.WriteSingleRegister(address, value);//写D0
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //读取型号
        public string ReadRegister(string str,int count)
        {
            if (IsConn == false)
            {
                return null;
            }
            string str1 = null;
            try
            {
                int address = DecodD(str);
                for (int i = address; i < address+count; i++)
                {
                    int[] value = mc.ReadHoldingRegisters(i, 1);//读D0
                    int a = value[0];
                    byte[] b = new byte[2];
                    b[0] = (byte)(a & 0xFF); // 低字节 H
                    b[1] = (byte)(a >> 8); // 高字节 Y
                    str1 = str1+Encoding.UTF8.GetString(b);
                    if (str1.Contains("\0"))
                    {
                        break;
                    }
                }
                str1 = str1.Replace("\0","");
                return str1;
            }
            catch (Exception)
            {
                return null;
            }
        }




        public static string ConvertRegistersToString(ushort[] registers, int offset, int stringLength)
        {
            byte[] array = new byte[stringLength];
            for (int i = 0; i < stringLength / 2; i++)
            {
                byte[] bytes = BitConverter.GetBytes(registers[offset + i]);
                array[i * 2] = bytes[0];
                array[i * 2 + 1] = bytes[1];
            }

            return Encoding.UTF8.GetString(array);
        }



        //M地址解析
        private int DecodM(string address)
        {
            string str = address.Substring(1);
            int value = Convert.ToInt32(str) + 8192;
            return value;
        }
        //D地址解析
        private int DecodD(string address)
        {
            string str = address.Substring(1);
            int value = Convert.ToInt32(str);
            return value;
        }

        //ASKII码解析 数字转字母
        public string DecodAskDA(int num)
        {
            string str = ((char)num).ToString();
            if (str == "Y")
            {
                str = "YH";
            }
            if (str == "W")
            {
                str = "WH";
            }
            return str;
        }

        //ASKII码解析 字母转数字
        public int DecodAskAD(char str)
        {
            int value = (int)str;
            return value;
        }


    }
}
